package com.lottery.material.util;

import com.lottery.material.common.EnumInfo;
import com.lottery.material.enumeration.base.BaseEnum;

import java.util.ArrayList;
import java.util.List;

/**
 * Description：枚举工具类
 * Created by 朱正磊 on 2019-04-15 17:49
 */
public class EnumUtil {

    /**
     * description：根据code和value以及枚举类Class对象获取description
     * user 朱正磊
     * time 2019-04-15 17:54
     * @param code 枚举code
     * @param value 枚举value
     * @param enumClass 枚举类Class对象
     * @param <T> 泛型
     * @return 返回枚举description
     */
    public static <T extends BaseEnum> String getDescriptionByCodeValue(String code, Object value, Class<T> enumClass) {
        for (T each: enumClass.getEnumConstants()) {
            if (each.getCode().equals(code) && each.getValue().equals(value)) {
                return each.getDescription();
            }
        }
        return null;
    }

    /**
     * description：获取枚举中的值和详情，用于jsp页面中查询列表数据展示
     * user 朱正磊
     * time 2019-04-15 17:55
     * @param code 枚举code
     * @param enumClass 枚举类Class对象
     * @param <T> 泛型
     * @return 返回指定code对应的数据字典
     */
    public static <T extends BaseEnum> List<EnumInfo> getEnumInfoList(String code, Class<T> enumClass) {
        List<EnumInfo> infoList = new ArrayList<>();
        for (T each: enumClass.getEnumConstants()) {
            if (each.getCode().equals(code)) {
                EnumInfo info = new EnumInfo();
                if (each.getValue() instanceof Boolean) {
                    info.setValue((Boolean) (each.getValue()) ? "1" : "0");
                } else {
                    info.setValue(each.getValue());
                }
                info.setDescription(each.getDescription());
                infoList.add(info);
            }
        }
        return infoList;
    }

}
